#include "../lvgl/lvgl.h"
#include "Mylvgl.h"
#include "lvgl.h"
#include <time.h>
#include "./HXD/HXD.h"
#include "./ESP_HT/ESP_HT.h"
#include <stdio.h>

// 开机启动界面 -> 功能选择界面 -> AI界面
//                               电量数据查询界面
//                               红外遥控界面

// uint8_t cmd_Match[] = {0x30,0x70,0xA0};
// uint8_t cmd_Control[] = {0x30,0x06,0x00,0x00,0x00};

int getFlag(int mode,int flag)
{
    static int temp = 0;
    if(mode == 1)
        temp = flag;
    return temp;
}


extern bool success_hongwai;
extern uint8_t h_1b;
extern uint8_t h_2b;

typedef struct 
{
    lv_obj_t *page;
    lv_timer_t *timer;
    lv_obj_t * msgbox;
}lv_page_t;


/*各个界面的句柄*/
static lv_obj_t * s_startPage = NULL;
static lv_page_t s_mainPage = {0};
static lv_page_t s_aiPage = {0};
static lv_page_t  s_menuPage = {0};
static lv_page_t  s_timerPage = {0};
static lv_page_t  s_controlPage = {0};
static lv_page_t  s_hongwaiPage = {0};

static lv_page_t  s_waitPage = {0};
static lv_page_t  s_successPage = {0};
static lv_page_t  s_failPage = {0};
static lv_page_t  s_wifiPage = {0};
static lv_page_t  s_systemPage = {0};
static lv_page_t s_chartVPage = {0};

static lv_page_t s_oldPage = {0};

// //创建所有页面
static void CreatePage(void)
{
    // 1. 创建页面
    if (s_mainPage.page == NULL)
        s_mainPage.page = lv_obj_create(NULL);

    if (s_aiPage.page == NULL)
        s_aiPage.page = lv_obj_create(NULL);

    if (s_menuPage.page == NULL)
        s_menuPage.page = lv_obj_create(NULL);

    if (s_timerPage.page == NULL)
        s_timerPage.page = lv_obj_create(NULL);
    
    if (s_controlPage.page == NULL)
        s_controlPage.page = lv_obj_create(NULL);

    if (s_hongwaiPage.page == NULL)
        s_hongwaiPage.page = lv_obj_create(NULL);

    if (s_chartVPage.page == NULL)
        s_chartVPage.page = lv_obj_create(NULL);

}

// 改为指针传递，直接操作全局页面结构体
static void DestoryPage(lv_page_t *page) {    
    if (page == NULL) return;

    // 删除消息框
    if (page->msgbox) {
        lv_obj_del(page->msgbox);
        page->msgbox = NULL;
    }

    // 删除定时器
    if (page->timer) {
        lv_timer_del(page->timer);
        page->timer = NULL;
    }

    // 删除页面对象
    if (page->page) {
        lv_obj_clean(page->page); // 关键：彻底删除对象
        // page->page = NULL;
    }
}



/*
    这个函数是拿来放置页面的返回键
    通过输入当前的界面 调用该函数 跳转到餐单界面
*/
static void event_back_cb(lv_event_t *e)
{
    lv_menu_page();
    // lv_show_page(s_menuPage.page);
}

void lv_btn_back(lv_obj_t *page)
{
    lv_obj_t * btn = lv_btn_create(page);
    //lv_obj_set_size(btn, 30, 20);
    lv_obj_set_size(btn, 50, 50);
    lv_obj_set_pos(btn, 5, 0);

    lv_obj_set_style_bg_opa(btn, LV_OPA_TRANSP, 0);
    lv_obj_set_style_border_width(btn, 0, 0);
    lv_obj_set_style_shadow_width(btn, 0, 0);
    lv_obj_add_event_cb(btn,event_back_cb, LV_EVENT_CLICKED,NULL);

    lv_obj_t * back = lv_label_create(btn);
    lv_obj_set_style_text_color(back,lv_color_hex(0xffffff),0);
    lv_obj_set_style_text_font(back, &lv_font_montserrat_20, 0);
    lv_label_set_text(back,LV_SYMBOL_LEFT);
    lv_obj_center(back);
}

/*
    这个函数是拿来放置时间的标签
*/
// lv_timer_t *timer;
// void update_time_timer(lv_timer_t  *task)
// {
//     static char time_str[9]; // 00:00:00\0
//     time_t now;
//     struct tm *time_info;

//     time(&now);
//     time_info = localtime(&now);

//     sprintf(time_str, "%02d:%02d:%02d", time_info->tm_hour, time_info->tm_min, time_info->tm_sec);

//     // 获取传递过来的 label，并更新文本
//     lv_label_set_text((lv_obj_t *)timer->user_data, time_str);
// }
// static void lv_time_show(lv_obj_t *page,int x,int y)
// {
//     // 创建一个标签对象来显示时间
//     lv_obj_t *label = lv_label_create(page);
//     lv_obj_set_style_text_color(label,lv_color_hex(0xffffff),0);
//     lv_obj_set_style_text_font(label,&lv_font_montserrat_20,0);
//     lv_label_set_text(label, "00:00:00");  // 初始文本
//     // lv_obj_set_pos(label,95,10); // 居中对齐
//     lv_obj_set_pos(label,x,y); // 居中对齐

//     // 创建 LVGL 定时器，每秒更新一次时间
//     page. = lv_timer_create(update_time_timer, 1000, label);
// }


/*
    这里是开机界面的展示
    下面是一些动画定时器的回调函数
*/
static void lv_start_page_finished_cb(lv_anim_t * a)
{
    if(0)
    {
        lv_menu_page();
    }

}

void lv_start_page(void)
{
    s_startPage = lv_obj_create(NULL);
    LV_IMG_DECLARE(zk);
    LV_FONT_DECLARE(lv_font_linshaoji);
    LV_FONT_DECLARE(receptacle_font);

    lv_obj_set_style_bg_color(s_startPage,lv_color_hex(0x000000),LV_PART_MAIN);
    lv_obj_set_style_bg_opa(s_startPage, LV_OPA_COVER, LV_PART_MAIN);


    lv_obj_t * img1 = lv_img_create(s_startPage);
    lv_img_set_src(img1, &zk);
    lv_obj_set_pos(img1,40,40);
    //lv_obj_align(img1, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_bg_color(img1,lv_color_hex(0x000000),0);

    lv_obj_t * label1 = lv_label_create(s_startPage);
    lv_obj_set_style_text_font(label1,&lv_font_linshaoji,0);
    lv_obj_align_to(label1,img1,LV_ALIGN_OUT_RIGHT_MID,0,-15);
    lv_obj_set_style_text_color(label1,lv_color_hex(0xffffff),0);
    //lv_obj_set_style_bg_opa(label1,LV_OPA_100,0);
    lv_label_set_text(label1,"622\n实验室");

    lv_obj_t * label2 = lv_label_create(s_startPage);
    lv_obj_set_style_text_font(label2,&lv_font_linshaoji,0);
    lv_obj_align_to(label2,img1,LV_ALIGN_OUT_BOTTOM_MID,-45,10);
    lv_obj_set_style_text_color(label2,lv_color_hex(0xffffff),0);
    //lv_obj_set_style_bg_opa(label1,LV_OPA_100,0);
    lv_label_set_recolor(label2, true);
    lv_label_set_long_mode(label2, LV_LABEL_LONG_WRAP); // 启用自动换行
    lv_label_set_text(label2,"#FFAD06 IVAP#智能插座");

    static lv_point_t line_pionts[] = { {25,40},{25,155} };
    static lv_style_t style_line;
    lv_style_init(&style_line);
    lv_style_set_line_width(&style_line,8);
    lv_style_set_line_color(&style_line,lv_color_hex(0xFFAD06));
    lv_style_set_line_rounded(&style_line, true);



    lv_obj_t *line = lv_line_create(s_startPage);
    //lv_obj_align_to(line,img1,LV_ALIGN_OUT_LEFT_MID,0,0);
    lv_line_set_points(line,line_pionts,2);
    lv_obj_add_style(line,&style_line,0);
    //lv_obj_set_pos(line,-100,100);

    lv_anim_t a1;
    lv_anim_init(&a1);

    lv_anim_set_var(&a1, line);
    lv_anim_set_time(&a1,500);
    lv_anim_set_delay(&a1,200);
    lv_anim_set_values(&a1,-115,0);
    lv_anim_set_path_cb(&a1,lv_anim_path_bounce);

    lv_anim_set_exec_cb(&a1, (lv_anim_exec_xcb_t)lv_obj_set_y); // 动画属性：X 坐标
    lv_anim_start(&a1);

    lv_anim_t a2;
    lv_anim_init(&a2);

    lv_anim_set_var(&a2, img1);
    lv_anim_set_time(&a2,1000);
    //lv_anim_set_delay(&a1,2000);
    lv_anim_set_values(&a2,-100,40);
    lv_anim_set_path_cb(&a2,lv_anim_path_bounce);

    lv_anim_set_exec_cb(&a2, (lv_anim_exec_xcb_t)lv_obj_set_x); // 动画属性：X 坐标
    lv_anim_start(&a2);

    lv_anim_t a3;
    lv_anim_init(&a3);

    lv_anim_set_var(&a3, label1);
    lv_anim_set_time(&a3,800);
    //lv_anim_set_delay(&a1,2000);
    lv_anim_set_values(&a3,-100,170);
    lv_anim_set_path_cb(&a3,lv_anim_path_bounce);

    lv_anim_set_exec_cb(&a3, (lv_anim_exec_xcb_t)lv_obj_set_x); // 动画属性：X 坐标
    lv_anim_start(&a3);

    lv_anim_t a4;
    lv_anim_init(&a4);

    lv_anim_set_var(&a4, label2);
    lv_anim_set_time(&a4,1000);
    //lv_anim_set_delay(&a1,2000);
    lv_anim_set_values(&a4,300,170);
    lv_anim_set_path_cb(&a4,lv_anim_path_bounce);

    lv_anim_set_exec_cb(&a4, (lv_anim_exec_xcb_t)lv_obj_set_y); // 动画属性：X 坐标

    lv_anim_set_ready_cb(&a4,lv_start_page_finished_cb); // 动画结束回调函数
    lv_anim_start(&a4);

    lv_obj_t *label_Init = lv_label_create(s_startPage);
    lv_obj_set_style_text_font(label_Init,&receptacle_font,0);
    // lv_obj_set_pos(label_Init,105,240);
    lv_obj_align(label_Init,LV_ALIGN_BOTTOM_MID,0,0);
    lv_label_set_text(label_Init,"初始化中...");

    lv_scr_load(s_startPage);
}


/*
    显示电量数据的界面
*/
void lv_chartV_page();

static void event_chart_cb(lv_event_t *e)
{
    lv_chartV_page();
}



lv_obj_t *label_v, *label_i, *label_p, *label_a,*label_T,*label_H;


static void update_data_cb(lv_timer_t * timer)
{
    LV_FONT_DECLARE(lv_font_linshaoji);
    lv_obj_set_style_text_font(label_v,&lv_font_linshaoji,0);
    lv_label_set_text_fmt(label_v, "电压:%sV", devstate.V);
    
    lv_obj_set_style_text_font(label_i,&lv_font_linshaoji,0);
    lv_label_set_text_fmt(label_i, "电流:%sA", devstate.I);
    
    lv_obj_set_style_text_font(label_p,&lv_font_linshaoji,0);    
    lv_label_set_text_fmt(label_p, "功率:%sW", devstate.P);
    
    lv_obj_set_style_text_font(label_a,&lv_font_linshaoji,0);    
    lv_label_set_text_fmt(label_a, "电费:%sKWH", devstate.E);

    lv_obj_set_style_text_font(label_a,&lv_font_linshaoji,0);    
    lv_label_set_text_fmt(label_T, "温度:%s度", devstate.temperature);

    lv_obj_set_style_text_font(label_a,&lv_font_linshaoji,0);    
    lv_label_set_text_fmt(label_H, "湿度:%sRH", devstate.humidity);
}

/*
    显示电量数据的界面
*/
void lv_main_page(void)
{   
    DestoryPage(&s_chartVPage);
    DestoryPage(&s_menuPage);
    // lv_obj_clean(s_menuPage.page);

    lv_mem_monitor_t mon;
    lv_mem_monitor(&mon);
    LV_LOG_USER("used: %d, free: %d, max used: %d", mon.total_size - mon.free_size, mon.free_size, mon.max_used);

    LV_FONT_DECLARE(lv_font_linshaoji);

    lv_obj_t * page = lv_obj_create(s_mainPage.page);
    lv_obj_set_size(page, 280, 240);               // 设置页面大小
    lv_obj_center(page);
    lv_obj_set_scroll_dir(page, LV_DIR_VER);       // 仅允许垂直滚动
    lv_obj_set_scroll_snap_y(page, LV_SCROLL_SNAP_NONE); // 禁用对齐滚动
    lv_obj_set_style_bg_color(page,lv_color_hex(0x000000),0);
    lv_obj_set_style_bg_opa(page, LV_OPA_COVER, LV_PART_MAIN);


    lv_btn_back(s_mainPage.page);

    lv_obj_t *obj1 = lv_btn_create(page);
    lv_obj_set_style_bg_color(obj1,lv_color_hex(0x000000),0);
    lv_obj_set_style_border_width(obj1,5,0);
    lv_obj_set_style_border_color(obj1,lv_color_hex(0xFFAD06),0);
    lv_obj_set_size(obj1,210,100);
    lv_obj_set_pos(obj1,55,10);

    label_v = lv_label_create(obj1);
    lv_obj_set_style_text_font(label_v,&lv_font_linshaoji,0);
    lv_obj_align(label_v,LV_ALIGN_CENTER,0,0);
    lv_obj_set_style_text_color(label_v,lv_color_hex(0xffffff),0);
    // lv_label_set_text_fmt(label_v,"电压:%d",devstate.V);
    lv_obj_set_style_radius(obj1, 10, 0); // 圆角大小

    lv_obj_t *obj2 = lv_btn_create(page);
    lv_obj_set_style_bg_color(obj2,lv_color_hex(0x000000),0);
    lv_obj_set_style_border_width(obj2,5,0);
    lv_obj_set_style_border_color(obj2,lv_color_hex(0xFFAD06),0);
    lv_obj_set_style_radius(obj2, 10, 0); // 圆角大小
    lv_obj_set_size(obj2,210,100);
    lv_obj_set_pos(obj2,55,120);

    label_i = lv_label_create(obj2);
    lv_obj_set_style_text_font(label_i,&lv_font_linshaoji,0);
    lv_obj_align(label_i,LV_ALIGN_CENTER,0,0);
    lv_obj_set_style_text_color(label_i,lv_color_hex(0xffffff),0);
    // lv_label_set_text_fmt(label_i,"电流:%d",devstate.I);

    lv_obj_t *obj3 = lv_btn_create(page);
    lv_obj_set_style_bg_color(obj3,lv_color_hex(0x000000),0);
    lv_obj_set_style_border_width(obj3,5,0);
    lv_obj_set_style_border_color(obj3,lv_color_hex(0xFFAD06),0);
    lv_obj_set_style_radius(obj3, 10, 0); // 圆角大小
    lv_obj_set_size(obj3,210,100);
    lv_obj_set_pos(obj3,55,230);
    lv_obj_add_event_cb(obj3,event_chart_cb,LV_EVENT_CLICKED,NULL);    

    label_p = lv_label_create(obj3);
    lv_obj_set_style_text_font(label_p,&lv_font_linshaoji,0);
    lv_obj_align(label_p,LV_ALIGN_CENTER,0,0);
    lv_obj_set_style_text_color(label_p,lv_color_hex(0xffffff),0);
    // lv_label_set_text_fmt(label_p,"功率:%d",power);


    lv_obj_t *obj4 = lv_btn_create(page);
    lv_obj_set_style_bg_color(obj4,lv_color_hex(0x000000),0);
    lv_obj_set_style_border_width(obj4,5,0);
    lv_obj_set_style_border_color(obj4,lv_color_hex(0xFFAD06),0);
    lv_obj_set_style_radius(obj4, 10, 0); // 圆角大小
    lv_obj_set_size(obj4,210,100);
    lv_obj_set_pos(obj4,55,340);

    label_a = lv_label_create(obj4);
    lv_obj_set_style_text_font(label_a,&lv_font_linshaoji,0);
    lv_obj_align(label_a,LV_ALIGN_CENTER,0,0);
    lv_obj_set_style_text_color(label_a,lv_color_hex(0xffffff),0);
    // lv_label_set_text_fmt(label_a,"电费:%d",energy);

    lv_obj_t *obj5 = lv_btn_create(page);
    lv_obj_set_style_bg_color(obj5,lv_color_hex(0x000000),0);
    lv_obj_set_style_border_width(obj5,5,0);
    lv_obj_set_style_border_color(obj5,lv_color_hex(0xFFAD06),0);
    lv_obj_set_style_radius(obj5, 10, 0); // 圆角大小
    lv_obj_set_size(obj5,210,100);
    lv_obj_set_pos(obj5,55,450);

    label_T = lv_label_create(obj5);
    lv_obj_set_style_text_font(label_T,&lv_font_linshaoji,0);
    lv_obj_align(label_T,LV_ALIGN_CENTER,0,0);
    lv_obj_set_style_text_color(label_T,lv_color_hex(0xffffff),0);
    // lv_label_set_text_fmt(label_T,"温度:%d",energy);

    lv_obj_t *obj6 = lv_btn_create(page);
    lv_obj_set_style_bg_color(obj6,lv_color_hex(0x000000),0);
    lv_obj_set_style_border_width(obj6,5,0);
    lv_obj_set_style_border_color(obj6,lv_color_hex(0xFFAD06),0);
    lv_obj_set_style_radius(obj6, 10, 0); // 圆角大小
    lv_obj_set_size(obj6,210,100);
    lv_obj_set_pos(obj6,55,560);

    label_H = lv_label_create(obj6);
    lv_obj_set_style_text_font(label_H,&lv_font_linshaoji,0);
    lv_obj_align(label_H,LV_ALIGN_CENTER,0,0);
    lv_obj_set_style_text_color(label_H,lv_color_hex(0xffffff),0);
    // lv_label_set_text_fmt(label_H,"湿度:%d",energy);    

    s_mainPage.timer = lv_timer_create(update_data_cb, 1000, NULL);  // 每1000ms刷新一次

    s_oldPage = s_mainPage;

    lv_scr_load(s_mainPage.page);
}

/*
    显示小智AI的界面
*/
static lv_obj_t * img_emoji  = NULL;
enum emoji
{
    Neutral,
    Smile,
    Angry,
    Sad,
    Loving,
    Think
};

/*这个函数用来定义各种图像，并且根据不同场景将图像表情显示出来*/
static void lv_image_show(int emoji)
{
    LV_IMG_DECLARE(neutral);
    LV_IMG_DECLARE(smile);
    LV_IMG_DECLARE(angry);
    LV_IMG_DECLARE(sad);
    LV_IMG_DECLARE(loving);
    LV_IMG_DECLARE(think);

    switch (emoji)
    {
        case Neutral:
        {
            lv_obj_t * img1 = lv_img_create(s_aiPage.page);
            lv_img_set_src(img1, &neutral);
            lv_obj_set_pos(img1,90,60);
            //lv_obj_align(img1, LV_ALIGN_CENTER, 0, 0);
            lv_obj_set_style_bg_color(img1,lv_color_hex(0x000000),0);
        }break;

        case Smile:
        {
            lv_obj_t * img1 = lv_img_create(s_aiPage.page);
            lv_img_set_src(img1, &smile);
            lv_obj_set_pos(img1,90,60);
            //lv_obj_align(img1, LV_ALIGN_CENTER, 0, 0);
            lv_obj_set_style_bg_color(img1,lv_color_hex(0x000000),0);
        }break;

        case Angry:
        {
            lv_obj_t * img1 = lv_img_create(s_aiPage.page);
            lv_img_set_src(img1, &angry);
            lv_obj_set_pos(img1,90,60);
            //lv_obj_align(img1, LV_ALIGN_CENTER, 0, 0);
            lv_obj_set_style_bg_color(img1,lv_color_hex(0x000000),0);
        }break;

        case Sad:
        {
            lv_obj_t * img1 = lv_img_create(s_aiPage.page);
            lv_img_set_src(img1, &sad);
            lv_obj_set_pos(img1,90,60);
            //lv_obj_align(img1, LV_ALIGN_CENTER, 0, 0);
            lv_obj_set_style_bg_color(img1,lv_color_hex(0x000000),0);
        }break;

        case Loving:
        {
            lv_obj_t * img1 = lv_img_create(s_aiPage.page);
            lv_img_set_src(img1, &loving);
            lv_obj_set_pos(img1,90,60);
            //lv_obj_align(img1, LV_ALIGN_CENTER, 0, 0);
            lv_obj_set_style_bg_color(img1,lv_color_hex(0x000000),0);
        }break;

        case Think:
        {
            lv_obj_t * img1 = lv_img_create(s_aiPage.page);
            lv_img_set_src(img1, &think);
            lv_obj_set_pos(img1,90,60);
            //lv_obj_align(img1, LV_ALIGN_CENTER, 0, 0);
            lv_obj_set_style_bg_color(img1,lv_color_hex(0x000000),0);
        }break;
    }
}

static int my_variable = 0; // 需要监控的外部变量,拿这个来判断ai有没有说话

String text1; // ai交互的文本
int speak_Flag;
static lv_obj_t * label_status = NULL;

// 定时器回调函数：检查变量是否变化，并更新 UI
static void update_ui(lv_timer_t * timer)
{
    static int last_value = -1; // 记录上一次的值，防止重复更新
    LV_FONT_DECLARE(receptacle_font);

    if (my_variable != last_value) // 变量变化时才更新 UI
    {
        lv_obj_t * label = lv_msgbox_get_text(s_aiPage.msgbox); // 获取消息框的标签对象
        lv_obj_set_style_text_font(label,&receptacle_font,0);
        lv_label_set_text(label, text1.c_str()); // 更新标签的文本
        speak_Flag = getFlag(0,0); // 获取speak_flag
        if(speak_Flag == 1)
        {
            lv_label_set_text(label_status,"说话中!");
            lv_image_show(Loving);
        }
        else
        {
            lv_label_set_text(label_status,"倾听中!");
            lv_image_show(Think);
        }
        // last_value = my_variable;
    }
}

void lv_AI_page(void)
{
    DestoryPage(&s_menuPage);

    LV_FONT_DECLARE(receptacle_font);
    lv_mem_monitor_t mon;
    lv_mem_monitor(&mon);
    LV_LOG_USER("used: %d, free: %d, max used: %d", mon.total_size - mon.free_size, mon.free_size, mon.max_used);
    // CreatePage();

    lv_btn_back(s_aiPage.page);
    lv_obj_set_style_bg_color(s_aiPage.page,lv_color_hex(0x000000),0);
    lv_obj_set_style_bg_opa(s_aiPage.page,LV_OPA_COVER,0);

    // lv_time_show(s_aiPage.page,105,0);

    // /*这里的作用是根据不同场景输出表情*/
    // lv_image_show(Neutral);

    // /*这里设置两个点代表线条的起始和结束的坐标*/
    // static lv_point_t line_pionts[] = { {0,40},{280,40} };
    // static lv_style_t style_line;
    // lv_style_init(&style_line);
    // lv_style_set_line_width(&style_line,4);
    // lv_style_set_line_color(&style_line,lv_color_hex(0xFFAD06));
    // lv_style_set_line_rounded(&style_line, true);

    // /*这里是画一条横线 将上下面分割开来*/
    // lv_obj_t *line = lv_line_create(s_aiPage);
    // //lv_obj_align_to(line,img1,LV_ALIGN_OUT_LEFT_MID,0,0);
    // lv_line_set_points(line,line_pionts,2);
    // lv_obj_add_style(line,&style_line,0);
    // //lv_obj_set_pos(line,-100,100);

    /*这里是两个图标 一个是wifi的一个是音量的*/
    lv_obj_t * WIFI = lv_label_create(s_aiPage.page);
    lv_obj_set_style_text_color(WIFI,lv_color_hex(0xffffff),0);
    lv_obj_set_style_text_font(WIFI, &lv_font_montserrat_20, 0);
    lv_label_set_text(WIFI,LV_SYMBOL_WIFI);
    lv_obj_set_pos(WIFI,220,0);

    lv_obj_t * Sound = lv_label_create(s_aiPage.page);
    lv_obj_set_style_text_color(Sound,lv_color_hex(0xffffff),0);
    lv_obj_set_style_text_font(Sound, &lv_font_montserrat_20, 0);
    lv_label_set_text(Sound,LV_SYMBOL_VOLUME_MAX);
    lv_obj_set_pos(Sound,250,0);


    img_emoji = lv_img_create(s_aiPage.page);

    /*这里判断当前ai的状态是听还是讲*/
    // if(speak_Flag == 0)
    // {
        label_status = lv_label_create(s_aiPage.page);
        lv_obj_set_style_text_font(label_status,&lv_font_montserrat_20,0);
        lv_obj_set_style_text_color(label_status,lv_color_hex(0xffffff),0);
        lv_obj_set_style_text_font(label_status,&receptacle_font,0);
        lv_label_set_text(label_status,"倾听中!");
        lv_obj_set_pos(label_status,180,90);

        // lv_image_show(Neutral);
    // }
    // else
    // {
    //     lv_obj_t * label2 = lv_label_create(s_aiPage.page);
    //     lv_obj_set_style_text_font(label2,&lv_font_montserrat_20,0);
    //     lv_obj_set_style_text_color(label2,lv_color_hex(0xffffff),0);
    //     lv_obj_set_style_text_font(label2,&receptacle_font,0);
    //     lv_label_set_text(label2,"讲话中！");
    //     lv_obj_set_pos(label2,180,90);

    //     lv_image_show(Smile);
    // }

    s_aiPage.msgbox = lv_msgbox_create(s_aiPage.page, NULL, "...", NULL, NULL);
    lv_obj_set_style_radius(s_aiPage.msgbox, 12, 0);  // 圆角半径
    lv_obj_set_style_pad_all(s_aiPage.msgbox, 10, 0);  // 四边内边距都设为10
    lv_obj_set_style_bg_color(s_aiPage.msgbox, lv_color_hex(0xffffff), 0);  // 背景深灰
    lv_obj_set_style_bg_opa(s_aiPage.msgbox,LV_OPA_COVER,0);

    lv_obj_set_size(s_aiPage.msgbox,240,100);
    lv_obj_set_pos(s_aiPage.msgbox,10,130);

    // 创建 LVGL 定时器，每 500ms 检测一次变量变化
    s_aiPage.timer = lv_timer_create(update_ui, 500, NULL);

    s_oldPage = s_aiPage;

    lv_scr_load(s_aiPage.page);
}

/*
    显示菜单遥控的界面
*/
static bool kaiguan = false;
lv_obj_t * btn_kq = NULL;
lv_obj_t * label_kq = NULL;


void lv_wifi_page()
{
    LV_FONT_DECLARE(receptacle_font);
    s_wifiPage.page = lv_obj_create(NULL);
    lv_obj_set_style_bg_color(s_wifiPage.page,lv_color_hex(0x000000),0);
    lv_btn_back(s_wifiPage.page);

    lv_obj_t * label_text = lv_label_create(s_wifiPage.page);

    lv_label_set_text(label_text,"设备配网中");
    lv_obj_set_style_text_font(label_text,&receptacle_font,0);
    lv_obj_set_style_text_color(label_text,lv_color_hex(0xffffff),0);
    lv_obj_center(label_text);

    lv_obj_t * label_ip = lv_label_create(s_wifiPage.page);
    lv_label_set_text(label_ip,"IP:192.168.5.11");
    lv_obj_set_style_text_color(label_ip,lv_color_hex(0xffffff),0);
    lv_obj_align_to(label_ip,label_text,LV_ALIGN_CENTER,0,20);

    s_oldPage = s_wifiPage;
    
    lv_scr_load(s_wifiPage.page);

}

static void event_kq_cb(lv_event_t *e)
{

    kaiguan = !kaiguan;

    if(!kaiguan)
    {
        lv_obj_set_style_bg_color(btn_kq, lv_color_hex(0xaa0000), 0);
        lv_label_set_text(label_kq, LV_SYMBOL_PLAY);
    }
    else
    {
        lv_obj_set_style_bg_color(btn_kq, lv_color_hex(0x00aa00), 0);
        lv_label_set_text(label_kq, LV_SYMBOL_PAUSE);
    }

}

static void event_Data_cb(lv_event_t * e)
{    
    lv_main_page();
    // lv_show_page(s_mainPage.page);    
}

static void event_AI_cb(lv_event_t * e)
{
    lv_AI_page();
}

static void event_Timer_cb(lv_event_t * e)
{
    lv_timer_page();
}

static void event_Hongwai_cb(lv_event_t * e)
{
    lv_hongwai_page();
}

static void event_System_cb(lv_event_t * e)
{
    lv_system_page();
}

static lv_obj_t * label_timer;  // 倒计时文本
lv_obj_t *label_NTP;
lv_obj_t *label_DJS;

static int hours = 0;
static int minutes = 0;
static int seconds = 0;

char buf[16];

// 定时器回调函数：检查变量是否变化，并更新 UI
static void update_status(lv_timer_t * timer)
{
    static int last_value = -1; // 记录上一次的值，防止重复更新
    LV_FONT_DECLARE(receptacle_font);

    if (my_variable != last_value) // 变量变化时才更新 UI
    {
        // if(devstate.socket == "1")
        // {
        //     lv_obj_set_style_bg_color(btn_kq, lv_color_hex(0xaa0000), 0);
        //     lv_label_set_text(label_kq, LV_SYMBOL_PLAY);
        // }
        // else
        // {
            lv_obj_set_style_bg_color(btn_kq, lv_color_hex(0x00aa00), 0);
            lv_label_set_text(label_kq, LV_SYMBOL_PAUSE);
        // }
        static char time_str[9]; // 00:00:00\0
        int tm_hour = hour();
        int tm_min = minute();
        int tm_sec = second();

        sprintf(time_str, "%02d:%02d:%02d", tm_hour, tm_min, tm_sec);
        sprintf(buf, "%02d:%02d:%02d", hours, minutes, seconds);
        lv_label_set_text(label_DJS, buf);
        // 获取传递过来的 label，并更新文本
        lv_label_set_text(label_NTP, time_str);
    }
}


void lv_menu_page(void)
{
    // timer_flag = 0;
    if(s_startPage != NULL)
    {
        lv_obj_clean(s_startPage);
    }
    if(s_waitPage.page != NULL)
    {
        lv_obj_clean(s_waitPage.page);
    }
    if(s_failPage.page != NULL)
    {
        lv_obj_clean(s_failPage.page);
    }
    if(s_successPage.page != NULL)
    {
        lv_obj_clean(s_successPage.page);
    }

    DestoryPage(&s_oldPage);
    lv_obj_clean(s_menuPage.page);  // 清空页面上的所有组件

    LV_IMG_DECLARE(Data);
    LV_IMG_DECLARE(Hongwai);
    LV_IMG_DECLARE(Timer);
    // LV_IMG_DECLARE(AI);
    LV_FONT_DECLARE(receptacle_font);

    // CreatePage();
    lv_mem_monitor_t mon;
    lv_mem_monitor(&mon);
    LV_LOG_USER("used: %d, free: %d, max used: %d", mon.total_size - mon.free_size, mon.free_size, mon.max_used);


    lv_obj_set_style_bg_color(s_menuPage.page,lv_color_hex(0x000000),0);
    lv_obj_set_style_bg_opa(s_menuPage.page, LV_OPA_COVER, 0);


    lv_obj_t * panel = lv_obj_create(s_menuPage.page);
    lv_obj_set_size(panel, 280, 240);
    lv_obj_set_style_bg_color(panel, lv_color_hex(0x000000), LV_PART_MAIN);
    lv_obj_set_style_border_color(panel, lv_color_hex(0x000000), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(panel, LV_OPA_COVER, 0);              // 设置背景不透明

    lv_obj_set_scroll_snap_x(panel, LV_SCROLL_SNAP_CENTER);
    // lv_obj_set_flex_flow(panel, LV_FLEX_FLOW_ROW);
    lv_obj_align(panel, LV_ALIGN_CENTER, 0, 0); // 居中显示
    lv_obj_add_flag(panel, LV_OBJ_FLAG_SCROLL_ONE);

    // lv_time_show(s_menuPage.page,105,0);

    const lv_img_dsc_t * image[4] = {&Data,&Hongwai,&Timer};
    // const char * text[5] = {"电量查询","红外遥控","倒计时","小智","系统"};
    const char * text[5] = {"电量查询","红外遥控","倒计时","系统"};
    lv_event_cb_t event[5] = {event_Data_cb,event_Hongwai_cb,event_Timer_cb,event_System_cb};

    uint8_t i;
    for(i = 0;i < 4;i ++)
    {
        if(i < 3)
        {
            lv_obj_t * btn = lv_btn_create(panel);
            lv_obj_set_size(btn, 80, 80);
            lv_obj_set_pos(btn, i * 100, 120);
            
            lv_obj_set_style_bg_color(btn, lv_color_hex(0xFFAD06), 0);
            lv_obj_set_style_bg_opa(btn, LV_OPA_COVER, 0);              // 设置背景不透明

            lv_obj_add_event_cb(btn, event[i], LV_EVENT_CLICKED, NULL);

            lv_obj_t * img = lv_img_create(btn);
            lv_img_set_src(img, image[i]);
            lv_obj_align(img, LV_ALIGN_CENTER, 0, 0);
            lv_obj_set_style_bg_color(img,lv_color_hex(0x000000),0);

            lv_obj_t *label = lv_label_create(panel);
            lv_obj_set_style_text_color(label,lv_color_hex(0xffffff),0);
            lv_obj_set_style_text_font(label,&receptacle_font,0);
            lv_label_set_text(label, text[i]);
            lv_obj_align_to(label,btn,LV_ALIGN_OUT_BOTTOM_MID,0,0);            
        }
        if(i == 3)
        {
            lv_obj_t * btn = lv_btn_create(panel);
            lv_obj_set_size(btn, 80, 80);
            lv_obj_set_pos(btn, i * 100, 120);
            lv_obj_set_style_bg_color(btn, lv_color_hex(0xFFAD06), 0);
            lv_obj_set_style_bg_opa(btn, LV_OPA_COVER, 0);              // 设置背景不透明

            lv_obj_add_event_cb(btn, event[i], LV_EVENT_CLICKED, NULL);

            lv_obj_t *label_system = lv_label_create(btn);
            lv_obj_set_style_text_color(label_system,lv_color_hex(0x000000),0);
            lv_obj_set_style_text_font(label_system,&lv_font_montserrat_48,0);
            lv_label_set_text(label_system, LV_SYMBOL_SETTINGS);
            lv_obj_align(label_system, LV_ALIGN_CENTER, 0, 0);

            lv_obj_t *label = lv_label_create(panel);
            lv_obj_set_style_text_color(label,lv_color_hex(0xffffff),0);
            lv_obj_set_style_text_font(label,&receptacle_font,0);
            lv_label_set_text(label, text[i]);
            lv_obj_align_to(label,btn,LV_ALIGN_OUT_BOTTOM_MID,0,0);
        }
    }

    btn_kq = lv_btn_create(s_menuPage.page);
    lv_obj_set_size(btn_kq, 75, 75);
    lv_obj_set_style_radius(btn_kq, LV_RADIUS_CIRCLE, 0);
    lv_obj_set_pos(btn_kq, 30, 30);
    lv_obj_add_event_cb(btn_kq,event_kq_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_set_style_bg_color(btn_kq, lv_color_hex(0xaa0000), 0);
    lv_obj_set_style_bg_opa(btn_kq, LV_OPA_COVER, 0);              // 设置背景不透明


    label_kq = lv_label_create(btn_kq);
    lv_obj_set_style_text_color(label_kq,lv_color_hex(0xffffff),0);
    lv_label_set_text(label_kq, LV_SYMBOL_PLAY);
    lv_obj_center(label_kq);

    // if(!kaiguan)
    // {
    //     lv_obj_set_style_bg_color(btn_kq, lv_color_hex(0xaa0000), 0);
    //     lv_label_set_text(label_kq, LV_SYMBOL_PLAY);
    // }
    // else
    // {
    //     lv_obj_set_style_bg_color(btn_kq, lv_color_hex(0x00aa00), 0);
    //     lv_label_set_text(label_kq, LV_SYMBOL_PAUSE);
    // }
    label_NTP = lv_label_create(s_menuPage.page);
    lv_obj_set_style_text_color(label_NTP,lv_color_hex(0xffffff),0);
    lv_obj_set_style_text_font(label_NTP,&lv_font_montserrat_20,0);
    lv_label_set_text(label_NTP, "00:00:00");  // 初始文本
    lv_obj_set_pos(label_NTP,95,10); // 居中对齐

    label_DJS = lv_label_create(s_menuPage.page);
    lv_obj_set_style_text_color(label_DJS,lv_color_hex(0xffffff),0);
    lv_obj_set_style_text_font(label_DJS,&lv_font_montserrat_20,0);
    lv_label_set_text(label_DJS, "00:00:00");  // 初始文本
    lv_obj_set_pos(label_DJS,150,50); // 居中对齐    

    s_menuPage.timer = lv_timer_create(update_status, 1000, 0);
    
    lv_scr_load(s_menuPage.page);
}

/*
    这里显示红外遥控的界面
*/
void lv_wait_page();
void lv_success_page();
void lv_fail_page();


static void event_hongwai_cb(lv_event_t * e)
{
    int num =  (int)(intptr_t)lv_event_get_user_data(e);
    switch(num)
    {
        case 1:
            LV_LOG_USER("pipe");
            ESPHT_Send(0x21);
            success_hongwai = false;
            lv_wait_page();
            break;
        case 2:
            LV_LOG_USER("yaokong");
            lv_control_page();
            break;
    }
}

void lv_hongwai_page()
{
    DestoryPage(&s_menuPage);

    LV_FONT_DECLARE(receptacle_font);
    DestoryPage(&s_hongwaiPage);

    // CreatePage();
    lv_mem_monitor_t mon;
    lv_mem_monitor(&mon);
    LV_LOG_USER("used: %d, free: %d, max used: %d", mon.total_size - mon.free_size, mon.free_size, mon.max_used);

    lv_obj_set_style_bg_color(s_hongwaiPage.page,lv_color_hex(0x000000),0);
    lv_obj_set_style_bg_opa(s_hongwaiPage.page, LV_OPA_COVER, 0);              // 设置背景不透明

    lv_btn_back(s_hongwaiPage.page);
    // lv_time_show(s_hongwaiPage.page,105,0);

    lv_obj_t * btn1 = lv_btn_create(s_hongwaiPage.page);
    lv_obj_set_size(btn1, 100, 100);
    lv_obj_set_style_bg_color(btn1, lv_color_hex(0xFFAD06), 0);
    lv_obj_set_style_bg_opa(btn1, LV_OPA_COVER, 0);              // 设置背景不透明
    lv_obj_set_pos(btn1,25,80);
    lv_obj_add_event_cb(btn1, event_hongwai_cb, LV_EVENT_CLICKED, (void*)(intptr_t)1);

    // 按钮上的文本
    lv_obj_t * label1 = lv_label_create(btn1);
    lv_obj_set_style_text_font(label1,&receptacle_font,0);
    lv_obj_set_style_text_color(label1,lv_color_hex(0xffffff),0);
    lv_label_set_text(label1, "匹配");
    lv_obj_center(label1);


    lv_obj_t * btn2 = lv_btn_create(s_hongwaiPage.page);
    lv_obj_set_size(btn2, 100, 100);
    lv_obj_set_style_bg_color(btn2, lv_color_hex(0xFFAD06), 0);
    lv_obj_set_style_bg_opa(btn2, LV_OPA_COVER, 0);              // 设置背景不透明
    lv_obj_set_pos(btn2,150,80);
    lv_obj_add_event_cb(btn2, event_hongwai_cb, LV_EVENT_CLICKED,(void*)(intptr_t)2);
    // 按钮上的文本
    lv_obj_t * label2 = lv_label_create(btn2);
    lv_obj_set_style_text_font(label2,&receptacle_font,0);
    lv_obj_set_style_text_color(label2,lv_color_hex(0xffffff),0);
    lv_label_set_text(label2, "遥控");
    lv_obj_center(label2);

    s_oldPage = s_hongwaiPage;
    lv_scr_load(s_hongwaiPage.page);
}

static void event_control_cb(lv_event_t * e)
{
    int num = (int)(intptr_t)lv_event_get_user_data(e);
    switch(num)
    {
        case 0:
            LV_LOG_USER("Power");
            ESPHT_Send(0x22);            
            break;
        case 1:
            LV_LOG_USER("Up");
            ESPHT_Send(0x23);  
            break;
        case 2:
            LV_LOG_USER("Down");
            ESPHT_Send(0x24);  
            break;
        case 3:
            LV_LOG_USER("26");
            ESPHT_Send(0x25);  
            break;
        // case 4:
        //     LV_LOG_USER("xxx");
        //     break;
        case 5:
            LV_LOG_USER("AUTO");
            ESPHT_Send(0x26);  
            break;
        case 6:
            LV_LOG_USER("ZL");
            ESPHT_Send(0x27);  
            break;
        case 7:
            LV_LOG_USER("CS");
            ESPHT_Send(0x28);  
            break;
        case 8:
            LV_LOG_USER("SF");
            ESPHT_Send(0x29);  
            break;
        case 9:
            LV_LOG_USER("JR");
            ESPHT_Send(0x2A);  
            break;
        case 10:
            LV_LOG_USER("JN");
            ESPHT_Send(0x2B);  
            break;
    }
}

void lv_control_page()
{
    // DestoryPage(&s_waitPage);
    DestoryPage(&s_failPage);


    lv_mem_monitor_t mon;
    lv_mem_monitor(&mon);
    LV_LOG_USER("used: %d, free: %d, max used: %d", mon.total_size - mon.free_size, mon.free_size, mon.max_used);
    
    LV_FONT_DECLARE(receptacle_font);
    
    lv_obj_t * label_sy = lv_label_create(s_controlPage.page);
    lv_obj_set_style_text_font(label_sy, &receptacle_font, 0);
    lv_obj_set_style_text_color(label_sy,lv_color_hex(0xffffff),0);
        
    lv_label_set_text_fmt(label_sy,"当前索引号");
    
    lv_obj_set_pos(label_sy,10,40);
    
    lv_obj_t * label_sy1 = lv_label_create(s_controlPage.page);
    lv_obj_set_style_text_font(label_sy1, &receptacle_font, 0);
    lv_obj_set_style_text_color(label_sy1,lv_color_hex(0xffffff),0);
    lv_label_set_text_fmt(label_sy1,"%02X%02X",h_1b,h_2b);
    lv_obj_set_pos(label_sy1,10,55);
    
    lv_obj_set_style_bg_color(s_controlPage.page,lv_color_hex(0x000000),0);
    lv_obj_set_style_bg_opa(s_controlPage.page, LV_OPA_COVER, 0);              // 设置背景不透明
    
    lv_btn_back(s_controlPage.page);
    // lv_time_show(s_controlPage.page,105,0);
    
    /* 电源键 */
    lv_obj_t * btn_pow = lv_btn_create(s_controlPage.page);
    lv_obj_set_size(btn_pow, 70, 70);
    lv_obj_set_style_bg_color(btn_pow, lv_color_hex(0xCC0000), 0);
    lv_obj_set_style_bg_opa(btn_pow, LV_OPA_COVER, 0);              // 设置背景不透明
    lv_obj_set_pos(btn_pow,110,30);
    lv_obj_add_event_cb(btn_pow, event_control_cb, LV_EVENT_CLICKED, (void*)(intptr_t)0);
    
    // 按钮上的文本
    lv_obj_t * label_btn = lv_label_create(btn_pow);
    lv_label_set_text(label_btn, LV_SYMBOL_POWER);
    lv_obj_set_style_text_color(label_btn,lv_color_hex(0xffffff),0);
    
    lv_obj_center(label_btn);
    
    /*温度上下键*/
    lv_obj_t * btn1 = lv_btn_create(s_controlPage.page);
    lv_obj_set_size(btn1, 60, 50);
    lv_obj_set_style_bg_color(btn1, lv_color_hex(0xFFAD06), 0);
    lv_obj_set_style_bg_opa(btn1, LV_OPA_COVER, 0);              // 设置背景不透明
    
    lv_obj_set_pos(btn1,25,80);
    lv_obj_add_event_cb(btn1, event_control_cb, LV_EVENT_CLICKED, (void*)(intptr_t)1);
    
    lv_obj_t * label1 = lv_label_create(btn1);
    lv_label_set_text(label1, LV_SYMBOL_PLUS);
    lv_obj_set_style_text_color(label1,lv_color_hex(0xffffff),0);
    
    lv_obj_center(label1);
    
    lv_obj_t * btn2 = lv_btn_create(s_controlPage.page);
    lv_obj_set_size(btn2, 60, 50);
    lv_obj_set_style_bg_color(btn2, lv_color_hex(0xFFAD06), 0);
    lv_obj_set_style_bg_opa(btn2, LV_OPA_COVER, 0);              // 设置背景不透明
    
    lv_obj_set_pos(btn2,25,170);
    lv_obj_add_event_cb(btn2, event_control_cb, LV_EVENT_CLICKED, (void*)(intptr_t)2);
    
    lv_obj_t * label2 = lv_label_create(btn2);
    lv_label_set_text(label2, LV_SYMBOL_MINUS);
    lv_obj_set_style_text_color(label2,lv_color_hex(0xffffff),0);
    
    lv_obj_center(label2);
    
    /*调到26度键*/
    lv_obj_t * btn3 = lv_btn_create(s_controlPage.page);
    lv_obj_set_size(btn3, 70, 40);
    lv_obj_set_style_bg_color(btn3, lv_color_hex(0x009900), 0);
    lv_obj_set_style_bg_opa(btn3, LV_OPA_COVER, 0);              // 设置背景不透明
    
    lv_obj_set_pos(btn3,200,50);
    lv_obj_add_event_cb(btn3, event_control_cb, LV_EVENT_CLICKED, (void*)(intptr_t)3);
    
    
    lv_obj_t * label3 = lv_label_create(btn3);
    lv_obj_set_style_text_font(label3, &receptacle_font, 0);
    lv_obj_set_style_text_color(label3,lv_color_hex(0xffffff),0);
    
    lv_label_set_text(label3, "26度");
    lv_obj_center(label3);
    
    /*暂未定义键*/
    // lv_obj_t * btn4 = lv_btn_create(s_controlPage);
    // lv_obj_set_size(btn4, 70, 40);
    // lv_obj_set_style_bg_color(btn4, lv_color_hex(0x009900), 0);
    // lv_obj_set_pos(btn4,200,40);
    // lv_obj_add_event_cb(btn4, event_control_cb, LV_EVENT_CLICKED, (void*)(intptr_t)4);
    
    
    // lv_obj_t * label4 = lv_label_create(btn4);
    // lv_obj_set_style_text_font(label3, &receptacle_font, 0);
    // lv_label_set_text(label4, "模式");
    // lv_obj_center(label4);
    
    const char * text[6] = {"自动","制冷","抽湿","送风","加热","节能"};
    
    for(int i = 0;i < 3;i++)
    {
        lv_obj_t * btn = lv_btn_create(s_controlPage.page);
        lv_obj_set_size(btn, 50, 40);
        lv_obj_set_style_bg_color(btn, lv_color_hex(0x009900), 0);
        lv_obj_set_style_bg_opa(btn, LV_OPA_COVER, 0);              // 设置背景不透明
        lv_obj_set_pos(btn,100 + i * 60,130);
        lv_obj_add_event_cb(btn, event_control_cb, LV_EVENT_CLICKED, (void*)(intptr_t)(5 + i));
        
        lv_obj_t * label = lv_label_create(btn);
        lv_obj_set_style_text_font(label, &receptacle_font, 0);
        lv_label_set_text(label, text[i]);
        lv_obj_center(label);
    }
    
    for(int i = 0;i < 3;i++)
    {
        lv_obj_t * btn = lv_btn_create(s_controlPage.page);
        lv_obj_set_size(btn, 50, 40);
        lv_obj_set_style_bg_color(btn, lv_color_hex(0x009900), 0);
        lv_obj_set_style_bg_opa(btn, LV_OPA_COVER, 0);              // 设置背景不透明
        lv_obj_set_pos(btn,100 + i * 60,200);
        lv_obj_add_event_cb(btn, event_control_cb, LV_EVENT_CLICKED, (void*)(intptr_t)(8 + i));
        
        
        lv_obj_t * label = lv_label_create(btn);
        lv_obj_set_style_text_font(label, &receptacle_font, 0);
        lv_label_set_text(label, text[3 + i]);
        lv_obj_center(label);
    }
    
    // for(int i = 0;i < 3;i++)
    // {
        //     lv_obj_t * btn = lv_btn_create(s_controlPage);
        //     lv_obj_set_size(btn, 50, 40);
        //     lv_obj_set_style_bg_color(btn, lv_color_hex(0x009900), 0);
        //     lv_obj_set_pos(btn,100 + i * 60,200);
        //     lv_obj_add_event_cb(btn, event_control_cb, LV_EVENT_CLICKED, (void*)(intptr_t)(11 + i));
        
        //     lv_obj_t * label = lv_label_create(btn);
        //     lv_obj_set_style_text_font(label, &receptacle_font, 0);
        //     lv_label_set_text(label, text[6 + i]);
        //     lv_obj_center(label);
        // }
        
        s_oldPage = s_controlPage;
        
        lv_scr_load(s_controlPage.page);
        // lv_obj_set_style_text_color(label_sy,lv_color_hex(0xffffff),0);
}

static void update_wait(lv_timer_t * timer)
{
    static uint16_t time = 0;
    // static bool success_hongwai = true; // 记录上一次的值，防止重复更新
    if (success_hongwai == true) // 变量变化时才更新 UI
    {
        lv_success_page(); // 更新 UI
        time++;
        // last_value = success_hongwai;
    }
    else
    {
        time++;
    }

    if(time > 10 && success_hongwai == true)
    {
        lv_timer_del(timer);
        time = 0;
        lv_control_page();
    }
    else if(time > 10 && success_hongwai == false)
    {        
        lv_fail_page();
        if(time > 12)
        {
            lv_timer_del(timer);
            time = 0;       
            lv_hongwai_page();  
        }

    }
}

void lv_wait_page()
{
    LV_FONT_DECLARE(receptacle_font);
    s_waitPage.page = lv_obj_create(NULL);
    lv_obj_set_style_bg_color(s_waitPage.page,lv_color_hex(0x000000),0);
    lv_obj_set_style_bg_opa(s_waitPage.page,LV_OPA_COVER,0);

    lv_obj_t *label = lv_label_create(s_waitPage.page);
    lv_obj_set_style_text_font(label, &receptacle_font, 0);
    lv_obj_set_style_text_color(label,lv_color_hex(0xffffff),0);
    lv_label_set_text(label,"请稍等...");
    lv_obj_set_align(label,LV_ALIGN_CENTER);

    lv_obj_t *label1 = lv_label_create(s_waitPage.page);
    lv_obj_set_style_text_color(label1,lv_color_hex(0xffffff),0);
    lv_label_set_text(label1,LV_SYMBOL_REFRESH);
    lv_obj_align_to(label1,label,LV_ALIGN_CENTER,0,20);

    s_waitPage.timer = lv_timer_create(update_wait, 500, NULL);

    lv_scr_load(s_waitPage.page);

}

void lv_success_page()
{
    if(s_successPage.page != NULL)
        lv_obj_del(s_successPage.page);
    LV_FONT_DECLARE(receptacle_font);
    s_successPage.page = lv_obj_create(NULL);
    lv_obj_set_style_bg_color( s_successPage.page,lv_color_hex(0x000000),0);
    lv_obj_set_style_bg_opa(s_successPage.page,LV_OPA_COVER,0);
    lv_btn_back( s_successPage.page);

    lv_obj_t *label_text = lv_label_create( s_successPage.page);
    lv_obj_set_style_text_font(label_text, &receptacle_font, 0);
    lv_obj_set_style_text_color(label_text,lv_color_hex(0xffffff),0);
    lv_label_set_text(label_text,"绑定成功");
    lv_obj_set_align(label_text,LV_ALIGN_CENTER);

    lv_obj_t *label_sy = lv_label_create( s_successPage.page);
    lv_obj_set_style_text_font(label_sy, &receptacle_font, 0);
    lv_obj_set_style_text_color(label_sy,lv_color_hex(0xffffff),0);
    lv_label_set_text_fmt(label_sy,"空调索引号:%02X%02X",h_1b,h_2b);
    lv_obj_align_to(label_sy,label_text,LV_ALIGN_CENTER,0,20);

    lv_scr_load( s_successPage.page);
}

void lv_fail_page()
{
     if(s_failPage.page != NULL)
        lv_obj_del(s_failPage.page);
    LV_FONT_DECLARE(receptacle_font);
    s_failPage.page = lv_obj_create(NULL);
    lv_obj_set_style_bg_color( s_failPage.page,lv_color_hex(0x000000),0);
    lv_obj_set_style_bg_opa(s_failPage.page,LV_OPA_COVER,0);

    // lv_btn_back( s_failPage.page);

    lv_obj_t *label_text = lv_label_create( s_failPage.page);
    lv_obj_set_style_text_font(label_text, &receptacle_font, 0);
    lv_obj_set_style_text_color(label_text,lv_color_hex(0xffffff),0);
    lv_label_set_text(label_text,"绑定失败");
    lv_obj_set_align(label_text,LV_ALIGN_CENTER);

    // s_oldPage = s_failPage;
    lv_scr_load(s_failPage.page);
}


/*
    这里展示定时器的界面
*/
/*
    这里展示定时器的界面
*/
// static lv_timer_t * countdown_timer = NULL; // 倒计时定时器
// static lv_obj_t * label_timer;  // 倒计时文本
// static lv_obj_t * btn_suspend;
static uint32_t countdown_value = 0; // 倒计时时间（可修改）

static lv_obj_t * sw1;
static lv_obj_t * sw2;

int num1 = 0; //倒计时的小时
int num2 = 0; //倒计时的分钟

// static int hours = 0;
// static int minutes = 0;
// static int seconds = 0;

// char buf[16];

static void event_roll1_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * obj = lv_event_get_target(e);
    if(code == LV_EVENT_VALUE_CHANGED) {
        char buf[32];
        lv_roller_get_selected_str(obj, buf, sizeof(buf));
        num1 = atoi(buf);  // 转换为整数
        LV_LOG_USER("Selected number1: %d\n", num1);
    }
}
static void event_roll2_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * obj = lv_event_get_target(e);
    if(code == LV_EVENT_VALUE_CHANGED) {
        char buf[32];
        lv_roller_get_selected_str(obj, buf, sizeof(buf));
        num2 = atoi(buf);  // 转换为整数
        LV_LOG_USER("Selected number2: %d\n", num2);
    }
}

static void update_time(lv_timer_t * timer)
{
    static int last_value = -1; // 记录上一次的值，防止重复更新
    LV_FONT_DECLARE(receptacle_font);

    if (my_variable != last_value) // 变量变化时才更新 UI
    {
        // 计算时、分、秒
        hours = devstate.countdown.toInt() / 3600;
        minutes = (devstate.countdown.toInt() % 3600) / 60;
        seconds = devstate.countdown.toInt() % 60;

        sprintf(buf, "%02d:%02d:%02d", hours, minutes, seconds);
        lv_label_set_text(label_timer, buf);
        if(countdown_value == 0)
        {
            lv_obj_clear_state(sw2, LV_STATE_CHECKED);
        }  
    }
}

static void switch_event2_cb(lv_event_t* e)
{
    if (lv_obj_has_state(sw2, LV_STATE_CHECKED)) 
    {       
         countdown_value = num1 * 3600 + num2 * 60; // 重置倒计时
        setSocketTimer(countdown_value,true);
    }
    else if(lv_obj_has_state(sw2, LV_STATE_CHECKED) == false)
    {
        countdown_value = 0;
        setSocketTimer(countdown_value,false);
    }
}

void lv_timer_page()
{
    // CreatePage();
    // timer_flag = 1;
    DestoryPage(&s_menuPage);
    lv_mem_monitor_t mon;
    lv_mem_monitor(&mon);
    LV_LOG_USER("used: %d, free: %d, max used: %d", mon.total_size - mon.free_size, mon.free_size, mon.max_used);

    // if (s_timerPage.page != NULL) 
    // {
    //     lv_obj_clean(s_timerPage.page);  // 清空页面上的所有组件
    // }

    lv_obj_set_style_bg_color(s_timerPage.page,lv_color_hex(0x000000),0);
    lv_obj_set_style_bg_opa(s_timerPage.page, LV_OPA_COVER, 0);              // 设置背景不透明


    lv_btn_back(s_timerPage.page);

    s_timerPage.timer = lv_timer_create(update_time, 500, NULL);

    // // 倒计时文本标签
    label_timer = lv_label_create(s_timerPage.page);
    lv_obj_set_pos(label_timer,110,20);
    lv_obj_set_style_text_color(label_timer,lv_color_hex(0xffffff),0);
    sprintf(buf, "%02d:%02d:%02d", hours, minutes, seconds);
    lv_label_set_text(label_timer, buf);


    // 创建开关
    sw2 = lv_switch_create(s_timerPage.page);
    lv_obj_set_size(sw2, 50, 20);
    lv_obj_set_pos(sw2, 180,190);

    // 设置轨道颜色（关闭时灰色）
    lv_obj_set_style_bg_color(sw2, lv_color_hex(0xcccccc), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(sw2, LV_OPA_COVER, LV_PART_MAIN);
    lv_obj_set_style_radius(sw2, LV_RADIUS_CIRCLE, LV_PART_MAIN);

    // 设置滑块样式
    lv_obj_set_style_bg_color(sw2, lv_color_hex(0xffffff), LV_PART_INDICATOR); // 滑块颜色
    lv_obj_set_style_radius(sw2, LV_RADIUS_CIRCLE, LV_PART_INDICATOR);

    // 设置打开时轨道颜色（绿色）
    lv_obj_set_style_bg_color(sw2, lv_color_hex(0x00cc66), LV_PART_INDICATOR | LV_STATE_CHECKED);
    lv_obj_set_style_bg_opa(sw2, LV_OPA_COVER, LV_PART_INDICATOR | LV_STATE_CHECKED);

    // 选中状态下滑块的颜色（也可以改变）
    lv_obj_set_style_bg_color(sw2, lv_color_hex(0xffffff), LV_PART_KNOB | LV_STATE_CHECKED);


    // 设置小圆点（滑块）的大小和样式
    lv_obj_set_style_bg_color(sw2, lv_color_hex(0xffffff), LV_PART_KNOB);
    lv_obj_set_style_radius(sw2, LV_RADIUS_CIRCLE, LV_PART_KNOB);
    lv_obj_set_style_pad_all(sw2, 2, LV_PART_KNOB);           // 缩小一点，给滑块留边距
    lv_obj_set_style_bg_opa(sw2, LV_OPA_COVER, LV_PART_KNOB);

    lv_obj_add_event_cb(sw2, switch_event2_cb, LV_EVENT_VALUE_CHANGED, NULL);

    LV_FONT_DECLARE(receptacle_font);

    lv_obj_t * label2 = lv_label_create(s_timerPage.page);
    lv_obj_set_style_text_color(label2,lv_color_hex(0xffffff),0);
    lv_obj_set_style_text_font(label2,&receptacle_font,0);
    lv_label_set_text(label2,"倒计时");
    lv_obj_align_to(label2,sw2,LV_ALIGN_OUT_BOTTOM_MID,0,10);

   


    lv_obj_t * roller1 = lv_roller_create(s_timerPage.page);
    lv_obj_t * roller2 = lv_roller_create(s_timerPage.page);

    lv_obj_t * label3 = lv_label_create(s_timerPage.page);
    lv_obj_t * label4 = lv_label_create(s_timerPage.page);

    lv_label_set_text(label3, "H");
    lv_obj_set_style_text_color(label3,lv_color_hex(0xffffff),0);
    lv_obj_align_to(label3,roller1,LV_ALIGN_OUT_RIGHT_MID,40,40);

    lv_label_set_text(label4, "M");
    lv_obj_set_style_text_color(label4,lv_color_hex(0xffffff),0);
    lv_obj_align_to(label4,roller2,LV_ALIGN_OUT_RIGHT_MID,135,40);

    lv_roller_set_options(roller1,
                          "0\n""1\n""2\n""3\n""4\n""5\n""6\n""7\n""8\n""9\n"
                          "10\n""11\n""12",
                          LV_ROLLER_MODE_NORMAL);

    lv_roller_set_options(roller2,
                          "0\n""1\n""2\n""3\n""4\n""5\n""6\n""7\n""8\n""9\n""10\n"
                          "11\n""12\n""13\n""14\n""15\n""16\n""17\n""18\n""19\n""20\n"
                          "21\n""22\n""23\n""24\n""25\n""26\n""27\n""28\n""29\n""30\n"
                          "31\n""32\n""33\n""34\n""35\n""36\n""37\n""38\n""39\n""40\n"
                          "41\n""42\n""43\n""44\n""45\n""46\n""47\n""48\n""49\n""50\n"
                          "51\n""52\n""53\n""54\n""55\n""56\n""57\n""58\n""59\n""60",
                          LV_ROLLER_MODE_NORMAL);

    // 设置尺寸使中间选中项正好居中
    lv_obj_set_size(roller1, 60, 120);
    lv_obj_set_size(roller2, 60, 120);


    // 主体样式
    lv_obj_set_style_bg_color(roller1, lv_color_hex(0x222222), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(roller1, LV_OPA_COVER, LV_PART_MAIN);
    lv_obj_set_style_text_font(roller1, &lv_font_montserrat_20, LV_PART_MAIN);
    lv_obj_set_style_text_color(roller1, lv_color_hex(0xffffff), LV_PART_MAIN);
    lv_obj_set_style_text_align(roller1, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);

    lv_obj_set_pos(roller1,50,40);
    lv_roller_set_selected(roller1, num1, LV_ANIM_OFF);
    lv_obj_add_event_cb(roller1, event_roll1_handler, LV_EVENT_ALL, NULL);

    // lv_roller_set_visible_row_count(roller2, 4);
    lv_obj_set_pos(roller2,150,40);
    lv_obj_set_style_bg_color(roller2, lv_color_hex(0x222222), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(roller2, LV_OPA_COVER, LV_PART_MAIN);
    lv_obj_set_style_text_font(roller2, &lv_font_montserrat_20, LV_PART_MAIN);
    lv_obj_set_style_text_color(roller2, lv_color_hex(0xffffff), LV_PART_MAIN);
    lv_obj_set_style_text_align(roller2, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN);

    lv_roller_set_selected(roller2, num2, LV_ANIM_OFF);
    lv_obj_add_event_cb(roller2, event_roll2_handler, LV_EVENT_ALL, NULL);

    // 选中项样式
    lv_obj_set_style_bg_color(roller1, lv_color_hex(0x808080), LV_PART_SELECTED);
    lv_obj_set_style_bg_opa(roller1, LV_OPA_COVER, LV_PART_SELECTED);
    lv_obj_set_style_text_font(roller1, &lv_font_montserrat_20, LV_PART_SELECTED);
    lv_obj_set_style_text_color(roller1, lv_color_hex(0x000000), LV_PART_SELECTED);

    lv_obj_set_style_bg_color(roller2, lv_color_hex(0x808080), LV_PART_SELECTED);
    lv_obj_set_style_bg_opa(roller2, LV_OPA_COVER, LV_PART_SELECTED);
    lv_obj_set_style_text_font(roller2, &lv_font_montserrat_20, LV_PART_SELECTED);
    lv_obj_set_style_text_color(roller2, lv_color_hex(0x000000), LV_PART_SELECTED);


    s_oldPage = s_timerPage;
    
    // memset(&s_oldPage, 0, sizeof(s_oldPage));

    lv_scr_load(s_timerPage.page);
}

uint8_t gonglv_06 = 7;
uint8_t gonglv_612 = 17;
uint8_t gonglv_1218 = 56;
uint8_t gonglv_1824 = 28;


static void draw_event_cb(lv_event_t * e)
{
    lv_obj_draw_part_dsc_t * dsc = lv_event_get_draw_part_dsc(e);
    if(!lv_obj_draw_part_check_type(dsc, &lv_chart_class, LV_CHART_DRAW_PART_TICK_LABEL)) return;

    if(dsc->id == LV_CHART_AXIS_PRIMARY_X && dsc->text) {
        const char * month[] = {"0-6", "6-12", "12-18", "18-24"};
        lv_snprintf(dsc->text, dsc->text_length, "%s", month[dsc->value]);
    }
}
static void event_chartback_cb(lv_event_t *e)
{
    // lv_show_page(s_mainPage.page);
    lv_main_page();
}

void lv_chartV_page(void)
{
    DestoryPage(&s_mainPage);
    LV_FONT_DECLARE(receptacle_font);

    lv_obj_t * chart;
    chart = lv_chart_create(s_chartVPage.page);
    lv_obj_set_size(chart, 200, 150);
    lv_obj_center(chart);
    lv_obj_set_style_bg_opa(chart, LV_OPA_COVER, 0);              // 设置背景不透明

    lv_chart_set_type(chart, LV_CHART_TYPE_BAR);
    lv_chart_set_range(chart, LV_CHART_AXIS_PRIMARY_Y, 0, 100);
    // lv_chart_set_range(chart, LV_CHART_AXIS_SECONDARY_Y, 0, 200);
    lv_chart_set_point_count(chart, 4);
    lv_obj_add_event_cb(chart, draw_event_cb, LV_EVENT_DRAW_PART_BEGIN, NULL);

    /*Add ticks and label to every axis*/
    lv_chart_set_axis_tick(chart, LV_CHART_AXIS_PRIMARY_X, 10, 5, 4, 1, true, 40);
    lv_chart_set_axis_tick(chart, LV_CHART_AXIS_PRIMARY_Y, 10, 5, 6, 2, true, 50);
    // lv_chart_set_axis_tick(chart, LV_CHART_AXIS_SECONDARY_Y, 10, 5, 3, 4, true, 50);

    /*Zoom in a little in X*/
    // lv_chart_set_zoom_x(chart, 800);

    /*Add two data series*/
    lv_chart_series_t * ser1 = lv_chart_add_series(chart, lv_palette_lighten(LV_PALETTE_GREEN, 2), LV_CHART_AXIS_PRIMARY_Y);
    lv_chart_series_t * ser2 = lv_chart_add_series(chart, lv_palette_darken(LV_PALETTE_GREEN, 2), LV_CHART_AXIS_SECONDARY_Y);

    // /*Set the next points on 'ser1'*/
    // lv_chart_set_next_value(chart, ser1, gonglv_06);
    // lv_chart_set_next_value(chart, ser1, gonglv_612);
    // lv_chart_set_next_value(chart, ser1, gonglv_1218);
    // lv_chart_set_next_value(chart, ser1, gonglv_1824);


    lv_coord_t * ser2_array = lv_chart_get_y_array(chart, ser2);
    /*Directly set points on 'ser2'*/
    ser2_array[0] = gonglv_06;
    ser2_array[1] = gonglv_612;
    ser2_array[2] = gonglv_1218;
    ser2_array[3] = gonglv_1824;

    lv_chart_refresh(chart); /*Required after direct set*/


    lv_obj_t * label_text = lv_label_create(s_chartVPage.page);
    lv_obj_set_style_text_font(label_text,&receptacle_font,0);
    lv_obj_align_to(label_text,chart,LV_ALIGN_OUT_TOP_MID,-30,-10);
    lv_label_set_text(label_text, "用户电量情况");


    lv_obj_t * btn = lv_btn_create(s_chartVPage.page);
    //lv_obj_set_size(btn, 30, 20);
    lv_obj_set_size(btn, 50, 50);
    lv_obj_set_pos(btn, 5, 0);

    lv_obj_set_style_bg_opa(btn, LV_OPA_TRANSP, 0);
    lv_obj_set_style_border_width(btn, 0, 0);
    lv_obj_set_style_shadow_width(btn, 0, 0);
    lv_obj_add_event_cb(btn,event_chartback_cb, LV_EVENT_CLICKED,NULL);

    lv_obj_t * back = lv_label_create(btn);
    lv_obj_set_style_text_color(back,lv_color_hex(0x000000),0);
    lv_obj_set_style_text_font(back, &lv_font_montserrat_20, 0);
    lv_label_set_text(back,LV_SYMBOL_LEFT);
    lv_obj_center(back);

    lv_scr_load(s_chartVPage.page);
}

static void event_system_cb(lv_event_t *e)
{
    Serial.println("haha");
}

void lv_system_page()
{
    DestoryPage(&s_menuPage);

    LV_FONT_DECLARE(receptacle_font);
    s_systemPage.page = lv_obj_create(NULL);
    lv_obj_set_style_bg_color(s_systemPage.page,lv_color_hex(0x000000),0);
    lv_obj_set_style_bg_opa(s_systemPage.page, LV_OPA_COVER, 0);

    lv_btn_back(s_systemPage.page);

    lv_obj_t * label_text = lv_label_create(s_systemPage.page);

    lv_label_set_text(label_text,"关于本机\n智能插座v1.0");
    lv_obj_set_style_text_font(label_text,&receptacle_font,0);
    lv_obj_set_style_text_color(label_text,lv_color_hex(0xffffff),0);
    lv_obj_set_pos(label_text,100,70);


    lv_obj_t * btn = lv_btn_create(s_systemPage.page);
    lv_obj_set_size(btn, 120, 70);
    
    // 使用暗红色代替纯红，更有“警告”感但不刺眼
    lv_obj_set_style_bg_color(btn, lv_color_hex(0xC0392B), 0);
    lv_obj_set_style_bg_opa(btn, LV_OPA_COVER, 0);
    
    // 圆角更柔和
    lv_obj_set_style_radius(btn, 12, 0);
    
    // 添加阴影效果，提升立体感
    lv_obj_set_style_shadow_width(btn, 10, 0);
    lv_obj_set_style_shadow_color(btn, lv_color_hex(0x96281B), 0);
    lv_obj_set_style_shadow_opa(btn, LV_OPA_50, 0);
    
    // 添加边框
    lv_obj_set_style_border_color(btn, lv_color_hex(0xA93226), 0);
    lv_obj_set_style_border_width(btn, 2, 0);
    
    lv_obj_set_pos(btn, 100, 150);
    lv_obj_add_event_cb(btn, event_system_cb, LV_EVENT_CLICKED, (void*)(intptr_t)0);
    
    // 文本样式
    lv_obj_t * label_btn = lv_label_create(btn);
    lv_label_set_text(label_btn, "恢复出\n厂模式");
    lv_obj_set_style_text_font(label_btn, &receptacle_font, 0);
    lv_obj_set_style_text_color(label_btn, lv_color_hex(0xFFFFFF), 0);
    lv_obj_center(label_btn);
    
    s_oldPage = s_systemPage;
    
    lv_scr_load(s_systemPage.page);

}



void GoPage(void)
{
    CreatePage();
    lv_start_page();
}